SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[paAgregarNorma]
@IdCola INT,
@TipoDeNorma VARCHAR(1),
@Rectificatoria BIT,
@Norma INT OUTPUT
AS
SET NOCOUNT ON
DECLARE @Fecha1 DATETIME
DECLARE @Usuario INT


SELECT @Fecha1 = Fecha1 FROM tb_ColaDeMovimientos WHERE IdCola = @IdCola
SELECT @Usuario = IdUsuario FROM tb_Usuarios WHERE  NTUser = USER_NAME()

BEGIN TRANSACTION NuevaNorma
-- SE CREA LA NORMA
INSERT tb_Normas
SELECT CONVERT(VARCHAR, UltimaNorma) +'-'+ CONVERT(VARCHAR, YEAR(GETDATE())), 1, 0, NULL, GETDATE(), NULL, CASE @TipoDeNorma WHEN 'L' THEN 3 WHEN 'F' THEN 5 WHEN 'B' THEN 2 WHEN 'A' THEN 1 END, CASE WHEN @Rectificatoria IS NULL THEN 0 WHEN @Rectificatoria = 0 THEN 0 ELSE 1 END, @Usuario FROM tb_MovimientosDocentes WHERE Mnemo = 'T'
SELECT @Norma = @@IDENTITY
IF @@Error <> 0 
  	GOTO Salir 


-- SE ACTUALIZA LA ULTIMA NORMA
UPDATE tb_MovimientosDocentes SET UltimaNorma = UltimaNorma + 1 WHERE Mnemo = 'T'
IF @@Error <> 0 
  	GOTO Salir 


Salir:

	IF @@Error <> 0
		ROLLBACK TRANSACTION NuevaNorma
	ELSE
		COMMIT TRANSACTION NuevaNorma
GO
GRANT EXECUTE ON  [dbo].[paAgregarNorma] TO [PersonalDocente]
GO
